from bs4 import BeautifulSoup
import requests
import pymysql
import time
import base64
import hashlib
from datetime import datetime, timedelta
import re
import sys
import getopt
import random

# con = pymysql.connect('114.215.192.224','jr1','JRyhdweb123','zhenmuwang',charset='utf8')
con = pymysql.connect('114.215.192.224','jr1','JRyhdweb123','quzizhu',charset='utf8')

cur = con.cursor()


def insert(sql,args=None):
    cur.execute(sql,args)
    con.commit()
    return cur.lastrowid

def select(sql):
    cur.execute(sql)
    return cur.fetchall()

def update(sql,args=None):
    cur.execute(sql,args)
    con.commit()
    return cur.lastrowid

def get_hash_str():
    """
    用来生成短连接
    """
    md5 = hashlib.md5()
    md5.update(str(time.time()).encode('utf-8'))
    digest = md5.digest()
    hash_str = str(base64.b64encode(digest),'utf-8')
    ret = []
    for i in hash_str:
        if  (i >= '0' and i <='9') or (i>='a' and i<='z') or (i>='A' and i<='Z'):
            ret.append(i)
            if len(ret) == 6:
                break
    print(ret)
    return ''.join(ret)

if __name__ == "__main__":
    data = select("select * from stpc_qzz_provider_bak order by id desc limit 50")
    for i in data:
        hash_str = get_hash_str()
        id ="update stpc_qzz_provider_bak set trad_place = %s where id = %s"

        rows = cur.execute(id, [('山东省临沂市', i[0])])

        con.commit()
        print(rows)
        print(id)